﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Testicide.Models;
using Testicide.DAL;

namespace Testicide.Controllers
{
    public class ProjectController : Controller
    {
        private TesticideContext db = new TesticideContext();

        //
        // GET: /Project/

        //[Authorize(Roles = "Admin,Friends")] means that only admin and friends can access
        //[Authorize] //this means that a user must be loggen in in order to access this action 
        public ActionResult Index()
        {
            Project p = new Project();
            ViewBag.project = p;
            return View(db.Project.ToList());
        }

        
        //
        // GET: /Project/Details/5

        public ActionResult Details(int id = 0)
        {
            Project project = db.Project.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Project/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Project project)
        {
            project.Date = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Project.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(project);
        }

        //
        // GET: /Project/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Project project = db.Project.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(project);
        }

        //
        // GET: /Project/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Project project = db.Project.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        //
        // POST: /Project/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Project project = db.Project.Find(id);
            List<Module> modules = project.Modules.ToList();
            //remove all modules in that project
            foreach(var item in modules)
            {
                List<Feature> features = item.Features.ToList();
                //remove all features in each module
                foreach(var item2 in features)
                {
                    List<Scenario> scenarios = item2.Scenarios.ToList();
                    //remove all scenarios in each feature
                    foreach(var item3 in scenarios)
                    {
                        db.Scenario.Remove(item3);
                    }
                    db.Feature.Remove(item2);
                }
                db.Module.Remove(item);
            }
            db.Project.Remove(project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        //
        //Search method
        //
        public ActionResult Search(string searchtxt)
        {
            var projects = from p in db.Project
                           select p;

            if (!String.IsNullOrEmpty(searchtxt))
            {
                projects = projects.Where(p => p.Name.ToUpper().Contains(searchtxt.ToUpper())
                                         || p.Url.ToUpper().Contains(searchtxt.ToUpper()));
            }
            if(projects.Count()==0)
            {
                ViewBag.Message = "The Project You are searching for doesn't exist";
            }
            else
            {
                ViewBag.Message = "";
            }
            return View(projects);
        }
        //
        //AutoComplete
        //
        public JsonResult AutoComplete(string term)
        {
            var result = db.Project.Where(p => p.Name.ToLower().Contains(term.ToLower()))
                .Select(p => p.Name).ToList();

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult AddEditRecord(int? id)
        {
            if (Request.IsAjaxRequest())
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Project project = db.Project.Where(m => m.ID == id).FirstOrDefault();
                    return PartialView("_ProjectData", project);
                }
                ViewBag.IsUpdate = false;
                return PartialView("_ProjectData");
            }
            else
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Project project = db.Project.Where(m => m.ID == id).FirstOrDefault();
                    return PartialView("ProjectData", project);
                }
                ViewBag.IsUpdate = false;
                return View("ProjectData");
            }
        }

        [HttpPost]
        public ActionResult AddEditRecord(Project project, string cmd)
        {
            if (ModelState.IsValid)
            {
                if (cmd == "Save")
                {
                    project.Date = DateTime.Now;
                    db.Project.Add(project);
                    db.SaveChanges();
                    return RedirectToAction("Index");                 
                }
                else
                {
                    Project pro = db.Project.Where(m => m.ID == project.ID).FirstOrDefault();
                    if (pro != null)
                    {
                        pro.Name = project.Name;
                        pro.Url = project.Url;
                        pro.Date = project.Date;
                        db.SaveChanges();
                    }
                    return RedirectToAction("Index");
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult DeleteRecord(int id)
        {
            Project project = db.Project.Find(id);
            List<Module> modules = project.Modules.ToList();
            //remove all modules in that project
            foreach (var item in modules)
            {
                List<Feature> features = item.Features.ToList();
                //remove all features in each module
                foreach (var item2 in features)
                {
                    List<Scenario> scenarios = item2.Scenarios.ToList();
                    //remove all scenarios in each feature
                    foreach (var item3 in scenarios)
                    {
                        db.Scenario.Remove(item3);
                    }
                    db.Feature.Remove(item2);
                }
                db.Module.Remove(item);
            }
            db.Project.Remove(project);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}